﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using PullSdkHelper;
using PullSdkHelper.Tables;
using Newtonsoft.Json;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {

            TblTimezone.TimeRange range = new TblTimezone.TimeRange(2359);
            Console.WriteLine(range);

            AuthorizeDoor door = (AuthorizeDoor)15;
            Console.WriteLine((door & AuthorizeDoor.Lock1) != 0);
            Console.WriteLine(IntPtr.Zero.ToInt64());

            //var dt = ZKSdk.ConvertDatetime(552663836);
            //Console.WriteLine(dt);
            //var t = ZKSdk.ConvertTimeInt(new DateTime(2000,2,29,1,1,1));
            //Console.WriteLine(t);

            //var dt2 = ZKSdk.ConvertDatetime(t);
            //Console.WriteLine(dt2);

            Task.Factory.StartNew(() =>
            {
                using (var device = DeviceCenter.TcpConnect("192.168.1.99", 4370))
                {
                    if (!device.IsConnected)
                    {
                        Console.WriteLine("连接失败");
                    }
                    else
                    {
                        Console.WriteLine("连接成功");
                        device.OpenDoor(1);
                        while (true)
                        {
                            try
                            {
                                var ret = device.GetRealStatus();
                                Console.WriteLine(JsonConvert.SerializeObject(ret));
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine("error:" + ex);
                            }
                            finally
                            {
                                Thread.Sleep(1000);
                                Console.WriteLine("---------------------");
                            }


                        }


                        //var ret = device.SetUserInfo(new TblUser()
                        //{
                        //    Pin = 1,
                        //    CardNo = 6456743,
                        //    Password = 654321,
                        //    Name = "ld1",
                        //    StartTime = DateTime.Today,
                        //    EndTime = DateTime.Today.AddYears(1)
                        //});
                        //if (ret >= 0)
                        //{
                        //    Console.WriteLine("成功更新ID为1的用户密码");
                        //}

                        //var newPwd = device.SetRandomPassword(1);
                        //Console.WriteLine(newPwd);

                        //var users = device.GetUsers();
                        //foreach (var user in users)
                        //{
                        //    var str = JsonConvert.SerializeObject(user);
                        //    Console.WriteLine(str);
                        //}


                        //device.OpenDoor(1, 15); //常开
                        //Thread.Sleep(5000);
                        //var ret = device.ColseDoor(1);
                        //Console.WriteLine(ret); 
                        //var ret = device.DisableAlwaysOpen(1);
                        //Console.WriteLine(ret);

                        //while (true)
                        //{
                        //var logs = device.GetLogs(true);
                        //foreach (var log in logs)
                        //{
                        //    var str = JsonConvert.SerializeObject(log);
                        //    Console.WriteLine(str);
                        //}
                        //Console.WriteLine("正在等待...");
                        //    Thread.Sleep(5000);


                        //    //var ret = device.GetRealLog();
                        //    //    Console.WriteLine(string.Join("\r\n",ret));
                        //    //    Console.WriteLine("正在等待...");
                        //    //    Thread.Sleep(5000);
                        //}

                        //device.OpenDoor(1);
                        //var ret = device.DisableAlwaysOpen(1);
                        //Console.WriteLine(ret);

                        //var ret = device.CancelAlarm();
                        //Console.WriteLine(ret);




                        //device.SaveData("user");
                        //device.SaveData("userauthorize");
                        //device.SaveData("holiday");
                        //device.SaveData("timezone");
                        //device.SaveData("transaction");
                        //device.SaveData("firstcard");
                        //device.SaveData("multimcard");
                        //device.SaveData("inoutfun");
                        //device.SaveData("firstcard");
                        //device.SaveData("templatev10");
                        //device.SaveData("fvtemplate");
                    }
                }
            });
            while (true)
            {
                Console.ReadKey();
            }
        }
    }
}
